package blogs.eidos.cloud.framework.common.api;

import io.swagger.annotations.ApiModelProperty;
import lombok.*;

/**
 * @author ：Eidos
 * @date ：Created in 2020/12/9
 * @description：返回类型封装
 * @modified By：
 * @version: 1.0
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Resp<T> {
    @ApiModelProperty(value = "状态码(200-成功, 0-失败)", example = "0", position = 1)
    @Getter
    @Setter
    private int code;
    @ApiModelProperty(value = "提示消息", example = "提示消息", position = 1)
    @Getter
    @Setter
    private String message;
    @ApiModelProperty(value = "数据集", position = 1)
    @Getter
    @Setter
    private T data;
    @Getter
    @Setter
    private Boolean whetherSuccess;

    public Resp(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public Resp(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> Resp<T> success(T data) {
        return new Resp<>(HttpCodeEnum.SUCCESS.getCode(), HttpCodeEnum.SUCCESS.getMessage(), data);
    }

    public static <T> Resp<T> success() {
        return new Resp<>(HttpCodeEnum.SUCCESS.getCode(), HttpCodeEnum.SUCCESS.getMessage());
    }

    public static <T> Resp<T> failed(String message) {
        return new Resp<>(HttpCodeEnum.INVOKE_FAILURE_ERROR.getCode(), message);
    }

    public static <T> Resp<T> failed() {
        return new Resp<>(HttpCodeEnum.INVOKE_FAILURE_ERROR.getCode(), HttpCodeEnum.INVOKE_FAILURE_ERROR.getMessage());
    }

    public static <T> Resp<T> failed(HttpCodeEnum httpCodeEnum) {
        return new Resp<>(httpCodeEnum.getCode(), httpCodeEnum.getMessage());
    }

    public static <T> Resp<T> failed(HttpCodeEnum httpCodeEnum, String message) {
        return new Resp<>(httpCodeEnum.getCode(), message);
    }

}
